<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工单号" prop="workOrder">
        <el-input
          v-model="queryParams.workOrder"
          placeholder="请输入工单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产线" prop="linename">
        <el-input
          v-model="queryParams.linename"
          placeholder="请输入产线"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['prodmanage:report:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUsebag"
          v-hasPermi="['prodmanage:report:edit']"
        >领包辅料</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handlePacking"
          v-hasPermi="['prodmanage:report:picking']"
        >打包</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['prodmanage:report:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['prodmanage:report:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['prodmanage:report:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reportList" @selection-change="handleSelectionChange" :default-sort="{prop: 'date', order: 'descending'}" ref="tableDatas">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="报工id" align="center" prop="reportId" sortable width="150"/> -->
      <el-table-column label="工单号" align="center" prop="workOrder" sortable width="150"/>
      <el-table-column label="打包状态" align="center" prop="packingCondition" :formatter="packingConditionFormat" sortable width="150"/>
      <el-table-column label="报工单号" align="center" prop="workReportno" sortable width="150"/>
      <el-table-column label="计划需求数量" align="center" prop="demandQuantity" sortable width="150"/>
      <el-table-column label="计划生产数量" align="center" prop="prodQuantity" sortable width="150"/>
      <el-table-column label="所属车间" align="center" prop="workshopname" sortable width="150"/>
      <el-table-column label="产线" align="center" prop="linename" sortable width="150"/>
      <el-table-column label="设备" align="center" prop="equipmentname" sortable width="150"/>
      <el-table-column label="产品代码" align="center" prop="productCode" sortable width="150"/>
      <el-table-column label="产品名称" align="center" prop="productName" sortable width="150"/>
      <el-table-column label="报工类型" align="center" prop="reportType" :formatter="reportTypeFormat" sortable width="150"/>
      <el-table-column label="报工数量" align="center" prop="reportAmount" sortable width="150"/>
      <el-table-column label="检验状态" align="center" prop="inspectionStatus" :formatter="inspectionStatusFormat" sortable width="150"/>
      <el-table-column label="报工时间" align="center" prop="reportTime" width="150" sortable>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reportTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报工人" align="center" prop="reportmanname" sortable width="150"/>
      <el-table-column label="审核状态" align="center" prop="auditStatus" :formatter="auditStatusFormat" sortable width="150"/>
      <el-table-column label="生产人员" align="center" prop="producername" sortable width="150"/>
      <el-table-column label="创建人" align="center" prop="createUser" sortable width="150"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="150" sortable>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="updateUser" sortable width="150"/>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="150" sortable>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="打印次数" align="center" prop="printsNum" sortable width="150"/>
      <el-table-column label="备注" align="center" prop="remark" sortable width="150"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['prodmanage:report:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['prodmanage:report:remove']"
          >删除</el-button>
            <el-button
              type="text"
              icon="el-icon-edit"
              size="mini"
              @click="handleCheck(scope.row)"
            >检验</el-button>
          <el-button
            type="text"
            icon="el-icon-edit"
            size="mini"
            @click="getCode(scope.row)"
          >条码</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改生产管理生产报工对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
        <el-col :span="6">
        <el-form-item label="工单号" prop="workOrder">
          <el-select
            v-model="form.workOrder"
            placeholder="请选择工单号"
            @change="workOrderChange"
          >
            <el-option
              v-for="dict in productionOrderOptions"
              :key="dict.prodorderNo"
              :label="dict.prodorderNo"
              :value="dict.prodorderNo"
            ></el-option>
          </el-select>
        </el-form-item>
        </el-col>
        <el-col :span="6">
        <el-form-item label="打包状态" prop="packingCondition">
          <el-select v-model="form.packingCondition" placeholder="请选择打包状态">
            <el-option
              v-for="dict in packingConditionOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        </el-col>
        <el-col :span="6">
        <el-form-item label="报工单号" prop="workReportno">
          <el-input v-model="form.workReportno" placeholder="请输入报工单号" :disabled="true"/>
        </el-form-item>
        </el-col>
        <el-col :span="6">
        <el-form-item label="产线" prop="linename">
          <el-input v-model="form.linename" placeholder="请输入产线" :disabled="true"/>
        </el-form-item>
        </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
        <el-form-item label="产品代码" prop="productCode">
          <el-input v-model="form.productCode" placeholder="请输入产品代码" :disabled="true"/>
        </el-form-item>
        </el-col>
        <el-col :span="6">
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" :disabled="true"/>
        </el-form-item>
        </el-col>
        <el-col :span="6">
        <el-form-item label="报工类型" prop="reportType">
          <el-select v-model="form.reportType" placeholder="请选择报工类型">
            <el-option
              v-for="dict in reportTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        </el-col>
        <el-col :span="6">
        <el-form-item label="报工数量" prop="reportAmount">
          <el-input v-model="form.reportAmount" placeholder="请输入报工数量" />
        </el-form-item>
        </el-col>
        </el-row>
        <el-row>
        <el-col :span="6">
        <el-form-item label="检验状态" prop="inspectionStatus">
          <el-select v-model="form.inspectionStatus" placeholder="请选择检验状态">
            <el-option
              v-for="dict in inspectionStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        </el-col>
        <el-col :span="6">
        <el-form-item label="报工时间" prop="reportTime">
          <el-date-picker clearable size="small" style="width: 160px"
            v-model="form.reportTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择报工时间">
          </el-date-picker>
        </el-form-item>
        </el-col>
        <el-col :span="6">
        <el-form-item label="报工人" prop="reportmanname">
          <el-input v-model="form.reportmanname" placeholder="请输入报工人" />
        </el-form-item>
        </el-col>
        <el-col :span="6">
        <el-form-item label="审核状态" prop="auditStatus">
          <el-select v-model="form.auditStatus" placeholder="请选择审核状态">
            <el-option
              v-for="dict in auditStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        </el-col>
        </el-row>
        <el-row>
        <el-col :span="6">
        <el-form-item label="设备" prop="equipmentname">
            <el-select
              v-model="form.equipmentname"
              placeholder="请选择设备"
            >
              <el-option
                v-for="dict in equipmentnameOptions"
                :key="dict.equipmentName"
                :label="dict.equipmentName"
                :value="dict.equipmentName"
              ></el-option>
            </el-select>
        </el-form-item>
        </el-col>
        <el-col :span="6">
        <el-form-item label="生产人员" prop="producername">
          <el-input v-model="form.producername" placeholder="请输入生产人员" />
        </el-form-item>
        </el-col>
        <el-col :span="6">
        <el-form-item label="打印次数" prop="printsNum">
          <el-input v-model="form.printsNum" placeholder="请输入打印次数" />
        </el-form-item>
        </el-col>
        <el-col :span="6">
        <el-form-item label="欲需求数" prop="demandQuantity">
          <el-input v-model="form.demandQuantity" placeholder="请输入计划需求数量" :disabled="true"/>
        </el-form-item>
        </el-col>
        </el-row>
        <el-row>
        <el-col :span="6">
        <el-form-item label="欲生产数" prop="prodQuantity">
          <el-input v-model="form.prodQuantity" placeholder="请输入计划生产数量" :disabled="true"/>
        </el-form-item>
        </el-col>
        <el-col :span="6">
        <el-form-item label="所属车间" prop="workshopname">
          <el-input v-model="form.workshopname" placeholder="请输入所属车间" :disabled="true"/>
        </el-form-item>
        </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="条码" :visible.sync="tmopen" width="500px" >
      <div >
        <img :src="tmurl"  class="login-code-img" width="100%" height="100%"/>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReport, getReport, delReport, addReport, updateReport, exportReport,getWorkOrder,setReportJy,getTmImg, Packing } from "@/api/prodmanage/report";
import {
  listprod
} from "@/api/prodmanage/up";
import { listPworkorder } from "@/api/prodplan/pworkorder";
import { listProdline } from "@/api/base/prodline";

export default {
  name: "Report",
  components: {
  },
  data() {
    return {
      //条码
      tmurl:"",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产管理生产报工表格数据
      reportList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //是否显示条码
      tmopen:false,
      // 打包状态字典
      packingConditionOptions: [],
      // 报工类型字典
      reportTypeOptions: [],
      // 检验状态字典
      inspectionStatusOptions: [],
      // 审核状态字典
      auditStatusOptions: [],
      //生产任务单
      productionOrderOptions: [],
      //设备列表，根据产线查的，所以必须有产线，且名字匹配
      equipmentnameOptions:[],
      //存放选中值的数组
      multipleTable:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workOrder: undefined,
        packingCondition: undefined,
        workReportno: undefined,
        lineId: undefined,
        productId: undefined,
        productName: undefined,
        reportType: undefined,
        shouldAmount: undefined,
        reportAmount: undefined,
        inspectionStatus: undefined,
        reportTime: undefined,
        reportMan: undefined,
        barCode: undefined,
        createUser: undefined,
        createTime: undefined,
        updateUser: undefined,
        updateTime: undefined,
        remark: undefined,
        isDel: undefined,
        auditStatus: undefined,
        equipment: undefined,
        producer: undefined,
        printsNum: undefined,
        demandQuantity: undefined,
        prodQuantity: undefined,
        workshop: undefined,
        source: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getWorkNo();
    this.getDicts("prodmanage_report_packing_condition").then(response => {
      this.packingConditionOptions = response.data;
    });
    this.getDicts("prodmanage_report_report_type").then(response => {
      this.reportTypeOptions = response.data;
    });
    this.getDicts("prodmanage_report_inspection_status").then(response => {
      this.inspectionStatusOptions = response.data;
    });
    this.getDicts("prodmanage_audit_status").then(response => {
      this.auditStatusOptions = response.data;
    });
  },
  methods: {
    /** 查询生产管理生产报工列表 */
    getList() {
      this.loading = true;
      this.queryParams.prodlineName=undefined;
      this.queryParams.workshop=undefined;
      listReport(this.queryParams).then(response => {
        this.reportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //查询生产任务单
    getWorkNo() {
      listprod().then((response) => {
        this.productionOrderOptions = response.rows;
      });
    },
    // 打包状态字典翻译
    packingConditionFormat(row, column) {
      return this.selectDictLabel(this.packingConditionOptions, row.packingCondition);
    },
    // 报工类型字典翻译
    reportTypeFormat(row, column) {
      return this.selectDictLabel(this.reportTypeOptions, row.reportType);
    },
    // 检验状态字典翻译
    inspectionStatusFormat(row, column) {
      return this.selectDictLabel(this.inspectionStatusOptions, row.inspectionStatus);
    },
    // 审核状态字典翻译
    auditStatusFormat(row, column) {
      return this.selectDictLabel(this.auditStatusOptions, row.auditStatus);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        reportId: undefined,
        workOrder: undefined,
        packingCondition: undefined,
        workReportno: undefined,
        lineId: undefined,
        productId: undefined,
        productName: undefined,
        reportType: undefined,
        shouldAmount: undefined,
        reportAmount: undefined,
        inspectionStatus: undefined,
        reportTime: undefined,
        reportMan: undefined,
        barCode: undefined,
        createUser: undefined,
        createTime: undefined,
        updateUser: undefined,
        updateTime: undefined,
        remark: undefined,
        isDel: undefined,
        auditStatus: undefined,
        equipment: undefined,
        producer: undefined,
        printsNum: undefined,
        demandQuantity: undefined,
        prodQuantity: undefined,
        workshop: undefined,
        source: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    //工单的选择事件
    workOrderChange(val)
    {
      this.getPwork(val);
      getWorkOrder(val).then(response => {
        this.form.workReportno=response;
      });

    },
    /** 查询工单信息 */
    getPwork(ordernum) {
      this.queryParams.orderNum=ordernum;
      listPworkorder(this.queryParams).then(response => {
        let pworkOrderForm = response.rows[0];
        this.form.linename = pworkOrderForm.prodLine;
        this.form.productName = pworkOrderForm.productName;
        this.form.demandQuantity = pworkOrderForm.quantity;
        this.form.prodQuantity = pworkOrderForm.needQuantity;
        this.form.productCode = pworkOrderForm.productCode;
        this.form.workshopname = pworkOrderForm.workshop;
        this.form.workshopname = pworkOrderForm.workshop;
        this.form.productId = pworkOrderForm.productId;
        this.getSheBei(pworkOrderForm.prodLine,pworkOrderForm.workshop);
      });
    },
    getSheBei(prodLine,workshop) {
      this.queryParams.prodlineName=prodLine;
      this.queryParams.workshop=workshop;
      listProdline(this.queryParams).then(response => {
        this.equipmentnameOptions = response.data;
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.reportId)
      this.single = selection.length!==1
      this.multiple = !selection.length
      this.multipleTable = selection;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产管理生产报工";
    },  
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const reportId = row.reportId || this.ids
      getReport(reportId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改生产管理生产报工";
      });
    },
    /** 检验按钮操作 */
    handleCheck(row) {
      const reportIds = row.reportId ;
      const workReportnoTs = row.workReportno;
      const jyts = row.inspectionStatus;
      if(jyts=="1" || jyts==1)
      {
        this.msgSuccess("已检验的不可以重复检验！");
        return;
      }
      this.$confirm('请确认生产管理生产报工单号为"' + workReportnoTs + '"的数据项检验?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return setReportJy(reportIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("检验成功");
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.reportId != null) {
            updateReport(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReport(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const reportIds = row.reportId || this.ids;
      this.$confirm('是否确认删除生产管理生产报工编号为"' + reportIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delReport(reportIds);
        }).then(response => {
          this.getList();
          this.msgSuccess(response.msg);
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有生产管理生产报工数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportReport(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    handlePacking(row){
      this.reset();
      var packingCondition = this.multipleTable.map(item => item.packingCondition)[0];
      var inspectionStatus = this.multipleTable.map(item => item.inspectionStatus)[0];
      var data =this.$refs.tableDatas.selection;
        // var tempArray=new Array();
        if (packingCondition == 1) {
          this.msgSuccess("已打包不能继续打包");
          return;
        }
        if (inspectionStatus != 1) {
          this.msgSuccess("已检品可以打包");
          return;
        }
        const reportId = row.reportId || this.ids
          Packing(reportId).then(response => {
            this.msgSuccess("打包成功");
            this.reset();
            this.getList();
          });
    },
    getCode(row) {
      const workReportnoTs = row.workReportno;
      //console.log("条码="+workReportnoTs);
      this.tmopen=true;
      getTmImg(workReportnoTs).then(res => {
        //console.log(JSON.stringify(res));
        this.tmurl = "data:image/gif;base64," + res.img;
      });
    },
  }
};
</script>
